package com.atguigu.gulimall.product.web;

import com.atguigu.gulimall.product.entity.CategoryEntity;
import com.atguigu.gulimall.product.entity.vo.CateJsonVo;
import com.atguigu.gulimall.product.service.CategoryService;
import org.redisson.api.RCountDownLatch;
import org.redisson.api.RLock;
import org.redisson.api.RReadWriteLock;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;
import java.util.UUID;

@Controller
public class IndexController {

    @Autowired
    private CategoryService categoryService;

    @Autowired
    StringRedisTemplate redisTemplate;

    @Autowired
    RedissonClient redissonClient;

    @GetMapping({"index.html", "/"})
    public String indexPage(Model model){
        List<CategoryEntity> leveCate = categoryService.getLeveCate();
        model.addAttribute("categorys",leveCate);
        return "index";
    }

    @GetMapping("index/catalog.json")
    @ResponseBody
    public Map<String, List<CateJsonVo>> getCateLogJson(Model model){
        Map<String, List<CateJsonVo>> cateJson = categoryService.getCateJson();
        return cateJson;
    }


    @GetMapping("read")
    @ResponseBody
    public void  read(){
        RReadWriteLock w_lock = redissonClient.getReadWriteLock("w_lock");
        RLock rLock = w_lock.readLock();
        rLock.lock();
        try {
            String s = redisTemplate.opsForValue().get("read");
            System.out.println(s);
        }catch (Exception e){
            throw new RuntimeException(e);
        }finally {
            rLock.unlock();
        }
    }

    @GetMapping("writ")
    @ResponseBody
    public void  writ(){
        RReadWriteLock w_lock = redissonClient.getReadWriteLock("w_lock");
        RLock rLock = w_lock.writeLock();
        String s = UUID.randomUUID().toString();
        try {
            rLock.lock();
            Thread.sleep(5000);
            redisTemplate.opsForValue().set("read",s);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }finally {
            rLock.unlock();
        }
    }

    @GetMapping("lockDock")
    @ResponseBody
    public String  lockDock() throws InterruptedException {
        RCountDownLatch door = redissonClient.getCountDownLatch("door");
        door.trySetCount(5);
        door.await();
        return "放假了。。。";
    }


    @GetMapping("goId/{id}")
    @ResponseBody
    public String  goId(@PathVariable("id") Long id){
        RCountDownLatch door = redissonClient.getCountDownLatch("door");
        door.countDown();
        return id+"班人走了";
    }

}
